<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Request;
use think\Validate;

class CarBrand extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $data = input();
        if ($data['brand_name']) {
            $map['brand_name'] = ['like',"%{$data['brand_name']}%"];
        }
        if ($data['arctic_id']){ // 车辆类型
            $map['arctic_id'] = $data['arctic_id'];
        }
        if ($this::$isdelete){
            $map['isdelete'] = 1;
        }else{
            $map['isdelete'] = 0;
        }

        $list = Db::name('car_brand')->where($map)
//            ->order('id','desc')
            ->order("brand_name",'desc')->paginate(10000);
        $count = $list->total();
        $arctic = Db::name('arctic')->field('id,name')->order('id','asc')->select();
        return view('',compact('list','count','arctic'));
    }

    /**
     * 添加页面
     */
    public function add(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post(); // 接收数据
            // 验证数据
            $rule = [
                'arctic_id'=>'require',
                'brand_name'=>'require',
            ];
            $message = [
                'arctic_id.require' => '车辆类型不能为空',
                'brand_name.require' => '车辆品牌不能为空',
            ];
            $validate = new Validate($rule,$message);
            $result = $validate->check($postData);
            if (!$result){
                return ajax_return_error($validate->getError());
            }
            // 保存数据
            $postData['create_time'] = time();
            $addId = Db::name('car_brand')->strict(false)->insertGetId($postData);
            if ($addId){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_error('添加失败');
            }
        }else{
            // 查询车型
            $arctic =  Db::name('arctic')->field('id,name')->select();
            return view('edit',compact('arctic'));
        }
    }

    /**
     * 编辑页面
     */
    public function edit(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post(); // 接收数据
            // 验证数据
            $rule = [
                'arctic_id'=>'require',
                'brand_name'=>'require',
            ];
            $message = [
                'arctic_id.require' => '车辆类型不能为空',
                'brand_name.require' => '车辆品牌不能为空',
            ];
            $validate = new Validate($rule,$message);
            $result = $validate->check($postData);
            if (!$result){
                return ajax_return_error($validate->getError());
            }
            // 保存数据
            $postData['create_time'] = time();
            $addId = Db::name('car_brand')->strict(false)->update($postData);
            if ($addId){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_error('添加失败');
            }
        }else{
            $data = input();
            // 查询汽车型号信息
            $vo = Db::name('car_brand')->where('id',$data['id'])->find();
            if (!$vo){
                return ajax_return_error('不存在该汽车型号信息');
            }
            // 查询车型
            $arctic =  Db::name('arctic')->field('id,name')->select();
            return view('edit',compact('arctic','vo'));
        }
    }

}
